lunoweather.cloud_height = tonumber(minetest.setting_get("cloud_height"));
--print("############## cloud_height = "..cloud_height)

--assert(minetest.add_particlespawner, "I told you to run the latest GitHub!")
--assert(minetest.get_heat, "I told you to run the latest GitHub!")

lunoweather.addvectors = function (v1, v2)
	return {x=v1.x+v2.x, y=v1.y+v2.y, z=v1.z+v2.z}
end

lunoweather.nodes_in_range = function(pos, search_distance, node_name)
	if pos==nil then return 0 end
	if pos.x==nil or type(pos.x)~="number" then return 0 end
	if pos.y==nil or type(pos.y)~="number" then return 0 end
	if pos.z==nil or type(pos.z)~="number" then return 0 end
	if search_distance==nil or type(search_distance)~="number" and search_distance<=0 then return 0 end
	if node_name==nil or type(node_name)~="string" and node_name=="" then return 0 end

	local minp = {x=pos.x-search_distance,y=pos.y-search_distance, z=pos.z-search_distance}
	local maxp = {x=pos.x+search_distance,y=pos.y+search_distance, z=pos.z+search_distance}
	local nodes = minetest.env:find_nodes_in_area(minp, maxp, node_name)
	--minetest.chat_send_all("Found (" .. node_name .. ": " .. #nodes .. ")")
	return #nodes
end

lunoweather.get_weather = function(pos)
	if pos==nil or pos.y==nil or type(pos.y)~="number" or pos.y<=0 or pos.y>=lunoweather.cloud_height then 
		return ""
	end
	if minetest.env:get_node_light(pos, 0.5) ~= 15 then  -- Make sure player is not in a cave/house...
		return ""
	end
	

	local lava_flowing = lunoweather.nodes_in_range(pos, 7, "default:lava_flowing")
	local lava_source = lunoweather.nodes_in_range(pos, 7, "default:lava_source")
	local water_flowing = lunoweather.nodes_in_range(pos, 6, "default:water_flowing")
	local water_source = lunoweather.nodes_in_range(pos, 6, "default:water_source")
	local dirt_with_snow = lunoweather.nodes_in_range(pos, 7, "default:dirt_with_snow")
	local snowblock = lunoweather.nodes_in_range(pos, 7, "default:snowblock")
	--minetest.chat_send_all("water_flowing="..water_flowing.. " water_source="..water_source)
	
	if lava_flowing>5 or lava_source>5 then
		return ""
	end
	
	if lunoweather.day =="" or lunoweather.day ~= lunoschedule.getMineDay() then
		lunoweather.day = lunoschedule.getMineDay()
		if 
			(lunoschedule.getMineMount() ==1 and math.random(1,lunoweather.frequency)<=lunoweather.frequency_rain_primavera)
			or (lunoschedule.getMineMount() ==2 and math.random(1,lunoweather.frequency)<=lunoweather.frequency_rain_verao)
			or (lunoschedule.getMineMount() ==3 and math.random(1,lunoweather.frequency)<=lunoweather.frequency_rain_outono)
			or (lunoschedule.getMineMount() ==4 and math.random(1,lunoweather.frequency)<=lunoweather.frequency_rain_inverno)
		then --A quantidade de chuva vaia de acordo com a estacao
			print("["..lunoschedule.getMineDay().."/"..lunoschedule.getMineMount().."] Iniciando dia de chuva...")
			minetest.chat_send_all("["..lunoschedule.getMineDay().."/"..lunoschedule.getMineMount().."] Iniciando dia de chuva...")
			lunoweather.weather = "rain"
			return lunoweather.weather
		elseif lunoschedule.getMineMount() ==4 then --Infervo sempre neva o tempo todo
			print("["..lunoschedule.getMineDay().."/"..lunoschedule.getMineMount().."] Iniciando dia de neve...")
			minetest.chat_send_all("["..lunoschedule.getMineDay().."/"..lunoschedule.getMineMount().."] Iniciando dia de neve...")
			lunoweather.weather = "snow"
			return lunoweather.weather
		else
			print("["..lunoschedule.getMineDay().."/"..lunoschedule.getMineMount().."] Iniciando clima limpo...")
			minetest.chat_send_all("["..lunoschedule.getMineDay().."/"..lunoschedule.getMineMount().."] Iniciando clima limpo...")
			lunoweather.weather = ""
			return lunoweather.weather
		end
	
		--[[
		if math.random(1,lunoweather.frequency)<=lunoweather.frequency_rainsnow then
			if lunoschedule.getMineMount() ==4 then	
			--if dirt_with_snow>=4 or snowblock>=4 then	
				--if lunoweather.weather ~= "snow" then minetest.chat_send_all("Iniciando neve...") end
				lunoweather.weather = "snow"
				return lunoweather.weather
			elseif lunoschedule.getMineMount() ==2 then	
				--if lunoweather.weather ~= "rain" then minetest.chat_send_all("Iniciando chuva...") end
				lunoweather.weather = "rain"
				return lunoweather.weather
			end
		elseif math.random(1,lunoweather.frequency)<=lunoweather.frequency_clearday then
			--if lunoweather.weather ~= "" then minetest.chat_send_all("Iniciando clima limpo...") end
			lunoweather.weather = ""
			return lunoweather.weather
		end
		]]--
	
		--lunoweather.weather = "rain" --Forcar a chuva
	end
	
	return lunoweather.weather --Caso nao seja nenhum dos climas, ele repete o clima anterior
end

